package com.gouzheng.controller;

import com.gouzheng.entity.Page;
import com.gouzheng.entity.Student;
import com.gouzheng.service.studentService;
import com.gouzheng.util.BaseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/student")
public class studentController {

    @Autowired
    private studentService studentService;

    //查询
    @RequestMapping("/findStudent")
    public ModelAndView findStudent(Integer superior, Integer deleteFlag,String stuName, Model model,HttpServletRequest request,HttpServletResponse response){
//        List<Student> list = studentService.findStudent(superior,0);
//        model.addAttribute("list",list);
        //new一个ModelAndView对象，通过addObject存储page和用户List并利用setViewName设定跳转页面
        ModelAndView modelAndView=new ModelAndView();
        int start=0;
        int count=3;
        try{
            //从前台获取 开始数据的索引
            start=Integer.parseInt(request.getParameter("page.start"));
            //从前台获取 每页展示的条目数
            count=Integer.parseInt(request.getParameter("page.count"));
        }catch (Exception e){
        }
        //将得到的值存储到page对象中
        Page page=new Page(start,count);
        //从数据库取出部分数据，可防止内存溢出
        List<Student> lists = studentService.getList(page.getStart(), page.getCount(),stuName,0);
       //获取student表学生总个数
        int total=studentService.getTotal();
        //获取student表中的总个数
        page.setTotal(total);
        //通过addObject存储page和list
        modelAndView.addObject("page",page);
        modelAndView.addObject("list",lists);
        modelAndView.addObject("stuName",stuName);
        //利用setViewName设定跳转页面
        modelAndView.setViewName("student");
        //返回modelAndView 跳转
        return modelAndView;
    }

//    //模糊查询
//    @RequestMapping("/fingStuName")
//    public String getStuName(String stuName,Model model){
//        List<Student> list = studentService.getStuName(stuName);
//        model.addAttribute("list",list);
//        return "student";
//    }

    //添加
    @RequestMapping("/addStudent")
    @ResponseBody
    public BaseBean addStudent(Student student){
        BaseBean baseBean=new BaseBean();
        studentService.addStudent(student);
        return baseBean;
    }

    //修改学生信息
    @RequestMapping("/ToupdateStudent")
    public String updateStudent(Model model,Integer stuId) throws Exception{
        Student student = studentService.findStuId(stuId);
        model.addAttribute("lists",student);
        return "updateStudent";
    }
    @RequestMapping("/updateStudent")
    public String updateStu(Student student){
        studentService.updateStudent(student);
        return "redirect:/student/findStudent";
    }
    //删除
    @RequestMapping("/deleteStu/{stuId}")
    public void  deleteStu(@PathVariable("stuId") Integer stuId, HttpServletResponse response, HttpServletRequest request) throws Exception{
        studentService.deleteStu(stuId);
        response.sendRedirect(request.getContextPath()+"/student/findStudent");
    }



}
